﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using Microsoft.Office.Core;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Graph = Microsoft.Office.Interop.Graph;
using System.Runtime.InteropServices;
using System.IO;
namespace DataLayer
{
    public class MindMap
    {
        
        XmlDocument doc;
        int resourceID;

        public int ResourceID
        {
            get { return resourceID; }
            set { resourceID = value; }
        }

        RootNode rootNode;

        public RootNode RootNode
        {
            get { return rootNode; }
            set { rootNode = value; }
        }

        private string GetRelativePath(string path)
        {
            string rs = "";
            int idex = path.IndexOf(@"Mindmaps\");
            if (idex > 0)
                rs = path.Substring(0, idex);
            return rs;
        }
        public MindMap(string _path)
        {
            doc = new XmlDocument();
            doc.Load(_path);            
            XmlNode rootnode = doc.FirstChild.FirstChild;
            this.rootNode = new RootNode();
            string relativePath = GetRelativePath(_path);
            rootNode.InitNode(rootnode, relativePath);            
        }
        public MindMap(string _path, ref int percent)
        {
            doc = new XmlDocument();
            doc.Load(_path);
            XmlNode rootnode = doc.FirstChild.FirstChild;
            this.rootNode = new RootNode();
            string relativePath = GetRelativePath(_path);
            rootNode.InitNode(rootnode, ref percent, relativePath);

        }

        public void CheckAndChangeNodesName(List<Node> compList)
        {
            List<Node> mapNodeList = new List<Node>();
            this.GetListNode(mapNodeList);
            foreach (Node mapItem in mapNodeList)
            {
                mapItem.CheckAndChangeName(compList);
            }
        }

        

        public void GetListNode(List<Node> listNode)
        {
            listNode.Add(rootNode);
            rootNode.FindDescendants(listNode);
        }

        
        public MindMap()
        {
            this.rootNode = new RootNode();
            doc = new XmlDocument();
        }

        public MindMap(Node _root)
        {
            RootNode newRoot = new RootNode();
            newRoot.CopyData(_root);
            newRoot.StrOrder = "1";
            this.rootNode = newRoot;
            doc = new XmlDocument();
        }

        public List<Node> FindNodesByLevel(int _level)
        {
            List<Node> rs = null;
            if (_level == 0)
            {
                rs = new List<Node>();
                rs.Add(rootNode);
            }
            else
            {
                rs = rootNode.FindNodesByLevel(_level-1);
            }
            return rs;
        }

        public List<Node> FindChildNodes(Node _parent)
        {
            return _parent.FindChildNodes();
        }

        public void InsertNode(int _level,Node newNode)
        {
            if (_level == 1)
            {
                this.rootNode.AddNode(newNode);
            }
            if (_level > 1)
            {
                this.rootNode.InsertNode(_level, newNode);
            }
        }
        public void InsertNode(Node _parent,Node newNode)
        {
            _parent.AddNode(newNode);
        }

        public void RemoveNode(string name)
        {
            this.rootNode.RemoveNode(name);
        }

        public List<Node> GetNodesAfter(Node _parent)
        {
            List<Node> rs = new List<Node>();
            _parent.GetNodesAfter(rs);
            return rs;
        }

        public Node GetNodeByName(string _name)
        { 
            return rootNode.GetNodeByName(_name);
        }

        public void SaveUpdate()
        {

        }

        public void CreateFormPP(string _ppPath)
        {

        }

        public void ExportInsert(string _path, string themeName, int index)
        {
            InsertExportProcess(_path, themeName, index);
        }

        public void ExportToPP(string _path, string themeName)
        {
            ExportProcess(_path, themeName);
        }

        public void ExportToPP(string _path, string themeName, int level)
        {
            ExportProcess(_path, themeName, level);
        }  

        public List<ErrorType> CheckError()
        {
            return null;
        }

        public XmlDocument MindmapToXmlDoc(string path)
        {
          rootNode.AddToXmlDoc(doc);            
            doc.Save(path);
            return doc;
        }

        public void createPP(string path, string themeName)
        {
            ExportProcess(path, themeName);
            
        }

        public void DelecteNode(Node _node)
        {
            if (rootNode.Name.Equals(_node.Name))
            {
                this.rootNode = null;
            }
            else
            {
                this.rootNode.DelecteNode(_node);
            }
        }

        public void ExportProcess(string _path,string themeName,int level)
        {
            String strTemplate, strPic;
            string path = Directory.GetCurrentDirectory();
            strTemplate = path +
              "/SystemFile/Blends.POT";
            strPic = path + "/SystemFile/GoneFishing.bmp";
            bool bAssistantOn;

            PowerPoint.Application objApp;
            PowerPoint.Presentations objPresSet;
            PowerPoint._Presentation objPres;
            PowerPoint.Slides objSlides;

            PowerPoint.SlideShowWindows objSSWs;
            PowerPoint.SlideShowTransition objSST;

            PowerPoint.SlideRange objSldRng;


            //Create a new presentation based on a template.
            objApp = new PowerPoint.Application();
            //objApp.Visible = MsoTriState.msoTrue;
            objPresSet = objApp.Presentations;

            objPres = objPresSet.Open2007(strTemplate,
                MsoTriState.msoFalse, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoTrue);
            
            string themePath = path + "/Resources/" + themeName + ".thmx";
            if (System.IO.File.Exists(themePath))
            {
                objPres.ApplyTheme(themePath);
                objSlides = objPres.Slides;
                int index = 1;
                this.rootNode.AddNodeToSlide(objSlides, ref index, 1, level);

                ////Wait for the slide show to end.
                objSSWs = objApp.SlideShowWindows;
                while (objSSWs.Count >= 1) System.Threading.Thread.Sleep(100);

                try
                {
                    objPres.SaveAs(_path, PowerPoint.PpSaveAsFileType.ppSaveAsOpenXMLPresentation);
                }
                catch
                {
                    MessageBox.Show("Target file is opening, please close it and try again");
                }
            }
            else
            {
                MessageBox.Show("Theme file is not found, please copy" + themeName + ".thmx to " + path + "/Resources");
            }
            
        }

        public void InsertExportProcess(string _path, string themeName, int startIndex)
        {
            if (!System.IO.File.Exists(_path))
            {
                MessageBox.Show("File is not exist");
                return;
            }
            String strTemplate, strPic;
            string path = Directory.GetCurrentDirectory();
            strTemplate = path +
              "/SystemFile/Blends.POT";
            strPic = path + "/SystemFile/GoneFishing.bmp";

            PowerPoint.Application objApp;
            PowerPoint.Presentations objPresSet;
            PowerPoint._Presentation objPres;
            PowerPoint.Slides objSlides;

            PowerPoint.SlideShowWindows objSSWs;

            //Create a new presentation based on a template.
            objApp = new PowerPoint.Application();
            //objApp.Visible = MsoTriState.msoTrue;
            objPresSet = objApp.Presentations;

            objPres = objPresSet.Open2007(_path,
                MsoTriState.msoFalse, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoTrue);
            string themePath = path + "/Resources/" + themeName + ".thmx";

            if (System.IO.File.Exists(themePath))
            {
                objPres.ApplyTheme(themePath);
                objSlides = objPres.Slides;
                if (startIndex > 0 && startIndex <= objSlides.Count)
                {
                    int index = startIndex;
                    ((Node)this.rootNode).AddNodeToSlide(objSlides, ref index);

                    ////Wait for the slide show to end.
                    objSSWs = objApp.SlideShowWindows;
                    while (objSSWs.Count >= 1) System.Threading.Thread.Sleep(100);

                    try
                    {
                        objPres.SaveAs(_path, PowerPoint.PpSaveAsFileType.ppSaveAsOpenXMLPresentation);
                    }
                    catch (Exception except)
                    {
                        MessageBox.Show(except.Data.ToString());
                    }
                }
                else
                {
                    MessageBox.Show("Slide index is invalid, please choose another index");
                }
            }
            else
            {
                MessageBox.Show("Theme file is not found, please copy " + themeName + ".thmx to " + path + "/Resources");
            }
            
        }
        public  void ExportProcess(string _path, string themeName)
        {
            String strTemplate, strPic;
            string path = Directory.GetCurrentDirectory();
            strTemplate = path +
              "/SystemFile/Blends.POT";
            strPic = path + "/SystemFile/GoneFishing.bmp";
            bool bAssistantOn;

            PowerPoint.Application objApp;
            PowerPoint.Presentations objPresSet;
            PowerPoint._Presentation objPres;
            PowerPoint.Slides objSlides;

            PowerPoint.SlideShowWindows objSSWs;
            PowerPoint.SlideShowTransition objSST;

            PowerPoint.SlideRange objSldRng;


            //Create a new presentation based on a template.
            objApp = new PowerPoint.Application();
            //objApp.Visible = MsoTriState.msoTrue;
            objPresSet = objApp.Presentations;
            
            objPres = objPresSet.Open2007(strTemplate,
                MsoTriState.msoFalse, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoTrue);
            string themePath = path + "/Resources/" + themeName + ".thmx";
            if (System.IO.File.Exists(themePath))
            {
                objPres.ApplyTheme(path + "/Resources/" + themeName + ".thmx");
                objSlides = objPres.Slides;
                int index = 1;
                this.rootNode.AddNodeToSlide(objSlides, ref index);

                ////Wait for the slide show to end.
                objSSWs = objApp.SlideShowWindows;
                while (objSSWs.Count >= 1) System.Threading.Thread.Sleep(100);

                try
                {
                    objPres.SaveAs(_path, PowerPoint.PpSaveAsFileType.ppSaveAsOpenXMLPresentation);
                }
                catch (Exception except)
                {
                    MessageBox.Show(except.Data.ToString());
                }
            }
            else
            {
                MessageBox.Show("Theme file is not found, please copy " + themeName + ".thmx to " + path + "/Resources");
            }
        } 
    }
}
